package leave.controller;

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import leave.dataaccess.DAOFactory;
import leave.dataaccess.EmployeeLeaveDAO;
import leave.dataaccess.LeaveDAO;
import leave.exception.DataAccessException;
import leave.model.Employee;
import leave.model.Leave;
import leave.model.impl.mysql.LeaveImpl;
import leave.status.LeaveStatus;

/**
 * Servlet implementation class ApproveLeavesServlet
 */
public class ApproveLeavesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ApproveLeavesServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String approve = request.getParameter("approve");
		String reject = request.getParameter("reject");
		Leave leave = new LeaveImpl();
		leave.setLeaveID(Integer.parseInt(request.getParameter("leaveID")));
		leave.setLeaveType(request.getParameter("leaveType"));
		int appliedEmpID = Integer.parseInt(request
				.getParameter("appliedEmpID"));
		// int approvedEmpID =
		// Integer.parseInt(request.getParameter("approvedEmpID"));
		//need to update employee id by login
		int approvedEmpID = 2;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
		String startDateString = request.getParameter("startDate");
		String endDateString = request.getParameter("endDate");
		leave.setEmployeeComment(request.getParameter("employeeComment"));
		leave.setContactDetails(request.getParameter("contactDetails"));
		leave.setWorkDissemination(request.getParameter("workDissemination"));
		leave.setManagerComments(request.getParameter("managerComment"));
		try {
			Date startDate = new Date(formatter.parse(startDateString)
					.getTime());
			Date endDate = new Date(formatter.parse(endDateString).getTime());
			leave.setStartDate(startDate);
			leave.setEndDate(endDate);

			LeaveDAO leaveDAO = DAOFactory.getInstance().getLeaveDAO();
			
			/*EmployeeLeaveDAO employeeDAO = DAOFactory.getInstance()
					.getEmployeeLeaveDAO();			

			Employee appliedEmployee = employeeDAO
					.getEmployeeByID(appliedEmpID);
			Employee approvedEmployee = employeeDAO
					.getEmployeeByID(approvedEmpID);

			leave.setAppliedBy(appliedEmployee);
			leave.setApprovedBy(approvedEmployee);*/

			if (approve != null) {
				leave.setStatus(LeaveStatus.APPROVED);
			} else if (reject != null) {
				leave.setStatus(LeaveStatus.REJECTED);
			}
			leaveDAO.updateLeaveStatus(leave.getLeaveID(), leave.getStatus().getStatusDescription(), approvedEmpID, leave.getManagerComments());
			response.sendRedirect("ApproveLeaves.jsp");
		} catch (DataAccessException ex) {
			ex.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
